package cn.xm.basic.service.impl;

import cn.xm.basic.mapper.BaseMapper;
import cn.xm.basic.query.BaseQuery;
import cn.xm.basic.service.BaseService;
import cn.xm.basic.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return (T) baseMapper.findById(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery baseQuery) {
        Long totals = baseMapper.queryCount(baseQuery);
        List<T> list = baseMapper.queryByPage(baseQuery);
        PageList<T> pageList = new PageList<>(totals, list);
        return pageList;
    }


    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }
}
